package com.cloudwise.controller;


import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Tester;
import com.cloudwise.entity.TesterVo;
import com.cloudwise.entity.User;
import com.cloudwise.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息表(User)表控制层
 *
 * @author
 * @since 2022-1-19 15:15:36
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    /**
     * @create by: wangcf
     * @description:登录方法
     * @create time: 2022/1/19 15:19
     * @param user
     * @return java.lang.String
     */
    @PostMapping("login")
    public ReturnBean login(@RequestBody User user){
        System.out.println("user:"+user.toString());
        //获取shiro的主体，subject
        Subject subject = SecurityUtils.getSubject();
        //构建用户登录的令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(), user.getPassword());
        //登录操作
        try {
            subject.login(token);
        } catch (UnknownAccountException e){
            //用户不存在
           return  super.fail(user,"用户名不存在");
        }catch (IncorrectCredentialsException ie)
        {
            return super.fail(user,"密码不正确");
        }
        //获取当前登录的用户
        Object principal = subject.getPrincipal();
        System.out.println(principal);
        return success(principal);
    }
    public ReturnBean touser(Long page  , Long limit, User user){
        List<User> userList = userService.list();
      return super.success(userList);
    }

}

